﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Yonkly.Models
{
    public class GroupViewData : BaseViewData
    {
        public GroupViewData(BaseViewData mybase)
            : base(mybase)
        {
            IsGroup = true;
        }


        public Group GroupInfo { get; set; }

        private List<GroupUserItem> approvedGroupMembers;
        public List<GroupUserItem> ApprovedGroupMembers
        {
            get
            {
                if (approvedGroupMembers == null)
                {
                    using (var dc = new YonklyDataContext())
                    {
                        approvedGroupMembers = GetGroupMembersByStatus(GroupMemberStatuses.Approved);
                    }
                }
                return approvedGroupMembers;
            }

        }


        private List<GroupUserItem> pendingApprovalGroupMembers;
        public List<GroupUserItem> PendingApprovalGroupMembers
        {
            get
            {
                if (pendingApprovalGroupMembers == null)
                {
                    pendingApprovalGroupMembers = GetGroupMembersByStatus(GroupMemberStatuses.Pending);
                }
                return pendingApprovalGroupMembers;
            }
        }

        private List<GroupUserItem> GetGroupMembersByStatus(GroupMemberStatuses status)
        {
            using (var dc = new YonklyDataContext())
            {
                return (from mem in dc.GroupMembers
                        where mem.Group == GroupInfo &&
                              mem.Status == (int)status
                        select new GroupUserItem()
                                   {
                                       UserId = mem.UserId,
                                       Email = mem.User.Membership.Email,
                                       UserName = mem.User.UserName,
                                       Role = (GroupMemberRoles)mem.Role,
                                       Status = (GroupMemberStatuses)mem.Status,
                                       GroupId = mem.GroupId
                                   }).ToList();
            }
        }


        private int myGroupStatus = -1;
        public GroupMemberStatuses MyGroupStatus
        {
            get
            {
                if (myGroupStatus == -1)
                {
                    using (var dc = new YonklyDataContext())
                    {
                        myGroupStatus = dc.GroupMembers.Where(m => m.GroupId == GroupInfo.Id
                                                   && m.UserId == LoggedInUser.UserId).First().Status;
                    }
                }
                return (GroupMemberStatuses)myGroupStatus;
            }
        }

        private int myGroupRole = -1;
        public GroupMemberRoles MyGroupRole
        {
            get
            {
                if (myGroupRole == -1)
                {
                    using (var dc = new YonklyDataContext())
                    {
                        myGroupRole = dc.GroupMembers.Where(m => m.GroupId == GroupInfo.Id
                                                   && m.UserId == LoggedInUser.UserId).First().Role;
                    }
                }
                return (GroupMemberRoles)myGroupRole;
            }
        }

        public PagedList<MessageItem> Messages { get; set; }
    }
}
